package com.otto.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otto.reggie.common.BaseContext;
import com.otto.reggie.common.R;
import com.otto.reggie.entity.OrderDetail;
import com.otto.reggie.entity.Orders;
import com.otto.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        log.info("页码：{}，容量：{}",page,pageSize);
        Page<OrderDetail> orderDetailPage = new Page<>(page, pageSize);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> ordersQueryWrapper = new LambdaQueryWrapper<>();
        ordersQueryWrapper.eq(Orders::getUserId,userId).orderByDesc(Orders::getCheckoutTime);
        List<Orders> orders = orderService.list(ordersQueryWrapper);
        Page<Orders> page1 = orderService.page(ordersPage, ordersQueryWrapper);
        return R.success(page1);
    }

    @GetMapping("/page")
    public R<Page> submit(int page, int pageSize){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> ordersQueryWrapper = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        ordersQueryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(ordersPage, ordersQueryWrapper);
        return R.success(ordersPage);
    }
}